package com.itheima.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 王加鑫
 * @Date 2021/5/28 11:03
 * @Version 1.0
 */
public class StudentTest {
    public static void main(String[] args) throws DocumentException {
        ArrayList<Student> students = new ArrayList<>();
        SAXReader reader = new SAXReader();
        //获取document对象
        Document document = reader.read(new File("day18_xml&注解&枚举/xml/Demo02.xml"));
        //获取根标签
        Element rootElement = document.getRootElement();
        //获取根标签下的所有子标签
        List<Element> elements = rootElement.elements();
        //遍历所有子标签
        for (Element e : elements) {
            //获取属性
            Attribute attribute = e.attribute("id");
            //获取属性的属性值
            String id = attribute.getValue();
            //获取子标签
            Element eName = e.element("name");
            //获取标签的属性值
            String name = eName.getText();
            Element eAge = e.element("age");
            String age = eAge.getText();
            students.add(new Student(id,name,Integer.parseInt(age)));
        }

        for (Student student : students) {
            System.out.println(student);
        }


    }
}
